﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace DALL.SysManage
{
    public class T_PUB_ROLEMENU
    {

        /// <summary>
        /// 获得角色菜单
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetRoleMenu(string strWhere)
        {
            StringBuilder varname1 = new StringBuilder();
            varname1.Append("SELECT t.ID        AS r_uid, \n");
            varname1.Append("       t.ROLE_ID   AS rid, \n");
            varname1.Append("       m.MENU_ID   AS mid, \n");
            varname1.Append("       m.MENU_NAME AS mname, \n");
            varname1.Append("       m.PARENT_ID AS pid,m.URL as url \n");
            varname1.Append("FROM   T_PUB_ROLEMENU t, \n");
            varname1.Append("       T_PUB_MENU m \n");
            varname1.Append("WHERE  t.MENU_ID = m.MENU_ID ");
            if (strWhere.Trim() != "")
            {
                varname1.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, varname1.ToString());

        }

        /// <summary>
        /// 删除角色菜单
        /// </summary>
        /// <param name="roel"></param>
        /// <param name="ids"></param>
        public void DeleteRoleMenul(string roel,string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_ROLEMENU ");
            strSql.Append(string.Format(" where ROLE_ID='{0}' and MENU_ID in ({1})", roel, ids));
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.SysManage.T_PUB_ROLEMENU> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ROLE_ID,MENU_ID ");
            strSql.Append(" FROM T_PUB_ROLEMENU ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.SysManage.T_PUB_ROLEMENU> list = new List<Model.SysManage.T_PUB_ROLEMENU>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.SysManage.T_PUB_ROLEMENU ReaderBind(IDataReader dataReader)
        {
            Model.SysManage.T_PUB_ROLEMENU model = new Model.SysManage.T_PUB_ROLEMENU();
            object ojb;
            ojb = dataReader["ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ID = (int)ojb;
            }
            ojb = dataReader["ROLE_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ROLE_ID = (int)ojb;
            }
            ojb = dataReader["MENU_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MENU_ID = (int)ojb;
            }
            return model;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.SysManage.T_PUB_ROLEMENU model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_ROLEMENU(");
            strSql.Append("ROLE_ID,MENU_ID)");

            strSql.Append(" values (");
            strSql.Append("@ROLE_ID,@MENU_ID)");
            strSql.Append(";select @@IDENTITY");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ROLE_ID", DbType.Int32, model.ROLE_ID);
            db.AddInParameter(dbCommand, "MENU_ID", DbType.Int32, model.MENU_ID);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return 0;
            }
            return result;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysManage.T_PUB_ROLEMENU GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,ROLE_ID,MENU_ID from T_PUB_ROLEMENU ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            Model.SysManage.T_PUB_ROLEMENU model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_ROLEMENU ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.Int32, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
